package com.itheima.tliasmanagementspringboot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tliasmanagementspringboot.common.PageBean;
import com.itheima.tliasmanagementspringboot.dtos.PageDto;
import com.itheima.tliasmanagementspringboot.dtos.StudentPageDto;
import com.itheima.tliasmanagementspringboot.mapper.ClazzMapper;
import com.itheima.tliasmanagementspringboot.mapper.StudentMapper;
import com.itheima.tliasmanagementspringboot.pojo.Student;
import com.itheima.tliasmanagementspringboot.service.ClazzService;
import com.itheima.tliasmanagementspringboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ClazzMapper clazzMapper;

    /**
     * 查询所有学员信息
     * @return 查询到的学员信息的集合
     */
    @Override
    public List<Student> queryAllStudentService() {

        return studentMapper.queryAllStudentMapper();
    }

    /**
     * 功能: 根据id查询学员信息
     * @return 查询到的学员信息
     */
    @Override
    public Student queryStudentByConditionService(Integer id) {
        return studentMapper.queryStudentByConditionMapper(id);
    }

    /**
     * 功能: 根据id删除学员信息

     */
    @Override
    public void deleteStudentByIds(Integer[] ids) {
        studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 功能: 添加/插入一条学员信息
     *
     * @param student 插入的学员数据
     */
    @Override
    public void insertStudentService(Student student) {


        //补全一些基础字段的信息(如:时间,密码,状态等等)
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount(0);
        student.setViolationScore(0);
        studentMapper.insertStudentMapper(student);
    }

    /**
     * 功能:根据id更新学员信息
     * @param student :学员id
     */
    @Override
    public void updateStudentByIdService(Student student) {

        studentMapper.updateStudentByIdMapper(student);
    }

    /**
     * 功能:分页查询学员信息
     * @param studentPageDto 请求参数
     * @return 返回的是封装后的查询到的数据集合和总条数的对象
     */
    @Override
    public PageBean<Student> pageQueryStudentService(StudentPageDto studentPageDto) {
        //传统方式

//        studentPageDto.setPage((studentPageDto.getPage() - 1) * studentPageDto.getPageSize());//处理分页的起始位置
//        //1.查数据集合
//        List<Student> students = studentMapper.PageQueryStudentListMapper(studentPageDto);
//
//        //2.查总条数
//        Integer total = studentMapper.PageQueryStudentTotalMapper(studentPageDto);
//
//        //3.将上面两个数据封装到PageBean中
//        return new PageBean(total,students);

        System.out.println("业务层:" + studentPageDto);
        //分页插件
        //1.开启分页,紧接着的第一个select会自动分页
        PageHelper.startPage(studentPageDto.getPage(), studentPageDto.getPageSize());

        //2.直接条件查询即可
        List<Student> students = studentMapper.pageQueryStudentListMapper(studentPageDto);
        Page<Student> p = (Page<Student>) students;

        //3.查询clazz表查到班级的名称
        students.forEach(s->{
            s.setClazzName(clazzMapper.queryClazzNameById(s.getClazzId()));
        });
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void violationHandlerService(Integer id, Integer score) {
        /*//score为0时候表示要清零
        if (score < 0) {
            throw new RuntimeException("分数不能为负数");
        }
        Student student = new Student();
        student.setId(id);
        student.setViolationScore(score);
        if (score == 0) {
            student.setViolationScore(score);
        }

        studentMapper.updateStudentByIdMapper(student);*/

        studentMapper.updateViolationMapper(id, score);
    }

    @Override
    public Map<String, List<Object>> queryCountGroupByClazzService() {
        //1.将班级和学员人数封装后统计出来
        List<Map<String, Integer>>  listMap = studentMapper.queryCountGroupByClazz();
        System.out.println("业务层" + listMap);
        //2.将班级信息和人数信息分别取出来放入List集合中
        Map<String, List<Object>> maps  = new HashMap<>();
        List<Object> dataList = new ArrayList<>();
        List<Object> categoryList = new ArrayList<>();
        listMap.forEach(m->{
            categoryList.add(m.get("name"));
            dataList.add(m.get("value"));
        });
        maps.put("categoryList", categoryList);
        maps.put("dataList", dataList);
        return maps;
    }
}
